//
//  InSoundFavouriteTool.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/3/25.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InSoundFavouriteTool.h"

#import <FMDB.h>

@implementation InSoundFavouriteTool

static FMDatabase *_dataBase = nil;

+ (NSString *)sadBox{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"userFavourite.sqlite"];
}


+ (FMDatabase *)shareDataBase {
    if (_dataBase == nil) {
        NSString *path = [self sadBox];
        
        NSLog(@"播放器fmdb %@",path);
        _dataBase = [FMDatabase databaseWithPath:path];
        
        if (_dataBase.open) {
            NSLog(@"播放器fmdb能打开,创建成功");
            
            
            NSString *sql = @"CREATE TABLE IF NOT EXISTS userFavourite (id INTEGER PRIMARY KEY AUTOINCREMENT,user_id TEXT,channelid TEXT,soundid TEXT,userid TEXT);";
            
            BOOL success = [_dataBase executeUpdate:sql];
            if (success) {
                NSLog(@"播放器fmdb创建表成功");
            }else {
                NSLog(@"播放器fmdb创建表失败");
                NSLog(@"播放器fmdb %@",_dataBase.lastErrorMessage);
            }
            
        }else {
            NSLog(@"播放器fmdb 创建数据库失败");
        }
    }
    return _dataBase;
}


+ (BOOL)selectchannel:(NSString *)channelid WithUser_id:(NSString *)user_id {
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE channelid = ? AND user_id = ?",channelid,user_id];
    //如果有了的话  是yes
    if(result.next) {
        NSLog(@"关注的频道中已经有此数据");
        return  YES;
    }else {
        return NO;
        }
        return nil;
    }



+ (BOOL)selectFriend:(NSString *)userid WithUser_id:(NSString *)user_id {
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE userid = ? AND user_id = ?",userid,user_id];
    //如果有了的话  是yes
    if(result.next) {
        NSLog(@"关注的用户中已经有此数据");
        return  YES;
    }else {
        return NO;
    }
    return nil;
}


+ (BOOL)selectSound:(NSString *)soundid WithUser_id:(NSString *)user_id {
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE soundid = ? AND user_id = ?",soundid,user_id];
    //如果有了的话  是yes
    if(result.next) {
        NSLog(@"关注的歌曲中已经有此数据");
        return  YES;
    }else {
        return NO;
    }
    return nil;

}

+ (BOOL)insertSound:(NSString *)soundid WithUser_id:(NSString *)user_id {
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE soundid = ? AND user_id = ?",soundid,user_id];
    
    if(result.next) {
        NSLog(@"关注歌曲已经有此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"INSERT INTO userFavourite (user_id,channelid,soundid,userid) VALUES (?,NULL,?,NULL)",user_id,soundid];
        
        if (success) {
            NSLog(@"关注歌曲插入成功");
        }else {
            NSLog(@"关注歌曲插入失败");
            NSLog(@"关注歌曲%@",[self shareDataBase].lastErrorMessage);
            
        }
        return success;
    }

}


+ (BOOL)insertChannel:(NSString *)channelid WithUser_id:(NSString *)user_id {
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE channelid = ? AND user_id = ?",channelid,user_id];
    
    if(result.next) {
        NSLog(@"关注频道已经有此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"INSERT INTO userFavourite (user_id,channelid,soundid,userid) VALUES (?,?,NULL,NULL)",user_id,channelid];
        
        if (success) {
            NSLog(@"关注频道插入成功");
        }else {
            NSLog(@"关注频道插入失败");
            NSLog(@"关注频道%@",[self shareDataBase].lastErrorMessage);
            
        }
        return success;
    }

}


+ (BOOL)insertUser:(NSString *)userid WithUser_id:(NSString *)user_id {
    
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE userid = ? AND user_id = ?",userid,user_id];
    
    if(result.next) {
        NSLog(@"关注用户已经有此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"INSERT INTO userFavourite (user_id,channelid,soundid,userid) VALUES (?,NULL,NULL,?)",user_id,userid];
        
        if (success) {
            NSLog(@"关注用户插入成功");
        }else {
            NSLog(@"关注用户插入失败");
            NSLog(@"关注用户%@",[self shareDataBase].lastErrorMessage);
            
        }
        return success;
    }
}


+ (BOOL)deleteChannel:(NSString *)channelid WithUser_id:(NSString *)user_id {
    
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE channelid = ? AND user_id = ?",channelid,user_id];
    
    if(!result.next) {
        NSLog(@"喜欢的频道无此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"DELETE FROM userFavourite WHERE channelid = ? AND user_id = ?",channelid,user_id];
        if (success) {
            NSLog(@"喜欢的频道删除成功");
        }else {
            NSLog(@"喜欢的频道删除失败");
            NSLog(@"喜欢的频道%@",[self shareDataBase].lastErrorMessage);
            
        }
        return success;
    }
}

+ (BOOL)deleteSound:(NSString *)soundid WithUser_id:(NSString *)user_id {
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE soundid = ? AND user_id = ?",soundid,user_id];
    
    if(!result.next) {
        NSLog(@"喜欢的声音无此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"DELETE FROM userFavourite WHERE soundid = ? AND user_id = ?",soundid,user_id];
        if (success) {
            NSLog(@"喜欢的声音删除成功");
        }else {
            NSLog(@"喜欢的声音删除失败");
            NSLog(@"喜欢的声音%@",[self shareDataBase].lastErrorMessage);
            
        }
        return success;
    }

}


+ (BOOL)deleteUser:(NSString *)userid WithUser_id:(NSString *)user_id {

    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM userFavourite WHERE userid = ? AND user_id = ?",userid,user_id];
    
    if(!result.next) {
        NSLog(@"关注的用户无此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"DELETE FROM userFavourite WHERE userid = ? AND user_id = ?",userid,user_id];
        if (success) {
            NSLog(@"关注的用户删除成功");
        }else {
            NSLog(@"关注的用户删除失败");
            NSLog(@"关注的用户%@",[self shareDataBase].lastErrorMessage);
            
        }
        return success;
    }

}

+ (void)closeDataBase {
    [[self shareDataBase]close];
}

@end
